<?php

class Weibo_model extends CI_Model{

	function __construct(){
        parent::__construct();
    }

	public function getHeaderInterest($id,$count = 5){
        $this->db->from("wb_interest");
		$this->db->select("interestscore");
        $this->db->where("uid", $id);
		$result = $this->db->get()->result_array();
		if(count($result)){
			$result = json_decode($result["0"]['interestscore'],true);
			array_multisort($result,SORT_DESC, SORT_STRING);
			return array_slice($result,0,$count);	
		}
		return false;
	}
	
	public function debug($array){
		echo "<pre>";
		print_r($array);
	}

	public function getUserRetweet($arrays){
        $this->db->from("wb_timeline");
		foreach($arrays as $key=>$id){
	        $this->db->or_where("wid", $id);
		}
		return $this->db->get()->result_array();
	}
	
	public function getUserInfo($arrays){
        $this->db->from("wb_user");
		foreach($arrays as $key=>$id){
	        $this->db->or_where("uid", $id);
		}
		return $this->db->get()->result_array();
	}

	public function getUserLeaderline($uid,$type,$page = 1){
		$start = ($page-1)*30;
		$count = 30;
		$time = date('Y-m-d').' 00:00:00';
		$sql = "SELECT t2 . * , t1.score FROM wb_usertimeline t1, wb_timeline t2 WHERE t2.contenttype!='ad' and t2.contenttype!='vote' and t2.contenttype!='event' and t1.wid =	t2.wid AND t1.uid =".$uid.
				" AND t1.type ='".$type."' and t2.created_time > '".$time."' ORDER BY  `t1`.`score` DESC LIMIT ".$start.",".$count;
		$query = $this->db->query($sql)->result_array();
		$retwid = array();
		$uids = array();
		
		foreach($query as $key=>$value){
			if($value['retweet']!=0){
				$retwid[]=$value['retweet'];
			}
			$uids[] = $value['uid'];
		}

		$retweibo = $this->getUserRetweet($retwid);
		$users = $this->getUserInfo($uids);

		foreach($query as $key=>$value){
			if($value['retweet']!=0){
				foreach($retweibo as $key1=>$res){
					if($value['retweet']==$res['wid']){
						$query[$key]['retweet'] = $res;
						break;
					}
				}
			}else{
				unset($query[$key]['retweet']);
			}

			foreach($users as $key2=>$res){
				if($value['uid']==$res['uid']){
					$query[$key]['user'] = $res;
					break;
				}
			}
		}
		return $query;
	}

	public function getCount($uid,$type){
		$time = date('Y-m-d').' 00:00:00';
		$sql = 'select count(*) from wb_usertimeline where type=\''.$type.'\' and uid = '.$uid.' and created_time>=\''.$time.'\'';
		$query = $this->db->query($sql)->result_array();
		return $query[0]['count(*)'];
	}		
}
?>